import {
	defineStore
} from 'pinia'
import {
	ref
} from 'vue'

export const useShoppingCart = defineStore('shoppingCart', () => {

	// 后端获取购物车数据，目前为空
	const shoppingCart = ref([])

	const shop = (data) => {

		// 如果购物车为空
		if (!shoppingCart.value.length) {
			shoppingCart.value.push(data)
			return
		}

		// 如果口味、菜品
		const b = shoppingCart.value.some(ele => ele.id == data.id && ele.desc == data.desc)
		if (b) {
			shoppingCart.value.find(e => e.id == data.id && e.desc == data.desc).num += data.num
		}
		else {
			shoppingCart.value.push(data)
		}


		updata()

	}

	// 删除shoppingCart指定数据
	const shopDelete = (i) => {
		shoppingCart.value.splice(i, 1)
		updata()
	}

	// 将shoppingCart数据上传至服务器
	const updata = () => {

	}


	return {
		shoppingCart,
		shop,
		shopDelete
	}
},
	{

		// 打开持久化，默认进行 localStorage 储存
		persist: true
	},
)